var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    phone: '', //电话
    name: '', //姓名
    plateNumber: '' //车牌号
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let info = JSON.parse(options.info);
    this.setData({
      name: info.name,
      plateNumber: info.plateNumber,
      phone: info.phone,
      id: info.id
    });
  },

  //手机号前端校验
  onPhoneEvent(e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ phone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ phone: e.detail.value.replace(/\D/g, '') });
    }
  },
  bindName(e) {
    this.setData({
      name: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },
  bindPlateNumber(e) {
    this.setData({
      plateNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, '')
    });
  },

  bindFormSubmit() {
    var that = this;
    var phone = that.data.phone;
    var name = that.data.name;
    var plateNumber = that.data.plateNumber;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!plateNumber) {
      wx.showToast({
        title: '车牌号必填',
        icon: 'none'
      });
      return false;
    } else if (!name) {
      wx.showToast({
        title: '客户姓名必填',
        icon: 'none'
      });
      return false;
    } else if (phone && !myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none'
      });
      return false;
    } else {
      wx.showLoading({ title: '提交中' });
      that.setData({ buthiddenSubmit: true });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateSiteVehicle',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          id: that.data.id,
          userId: app.globalData.userId,
          phone: phone,
          name: name,
          plateNumber: plateNumber //车牌号
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.code == 100) {
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 1000
            });
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              });
            }, 1500);
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            that.setData({ buthiddenSubmit: false });
          }
        },
        fail: function(error) {
          that.setData({ buthiddenSubmit: false });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
